package com.mindskip.xzs.repository;

import com.mindskip.xzs.domain.ExamPaper;
import com.mindskip.xzs.domain.ExamPaperAnswer;
import com.mindskip.xzs.domain.other.KeyValue;
import com.mindskip.xzs.repository.base.MyMapper;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperPageRequestVM;
import com.mindskip.xzs.viewmodel.student.dashboard.PaperFilter;
import com.mindskip.xzs.viewmodel.student.dashboard.PaperInfo;
import com.mindskip.xzs.viewmodel.student.exam.ExamPaperPageVM;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Mapper
@Repository
public interface ExamPaperMapper extends MyMapper<ExamPaper> {
//    int deleteByPrimaryKey(Integer id);

//    int insert(ExamPaper record);

//    int insertSelective(ExamPaper record);

//    ExamPaper selectByPrimaryKey(Integer id);

//    int updateByPrimaryKeySelective(ExamPaper record);

//    int updateByPrimaryKey(ExamPaper record);

    String queryParam = " id, name, subject_id subjectId, paper_type paperType, grade_level gradeLevel, score, question_count questionCount, suggest_time suggestTime, " +
            "limit_start_time limitStartTime, limit_end_time limitEndTime, frame_text_content_id frameTextContentId, create_user createUser, create_time createTime, " +
            "deleted, task_exam_id taskExamId ";

    @Select("<script>" +
            "select " + queryParam +
            " FROM t_exam_paper where deleted=0" +
            "<if test=\"id != null \"> and id= #{id}</if> " +
            "<if test=\"level != null \"> and grade_level= #{level}</if>" +
            "<if test=\"subjectId != null \"> and subject_id= #{subjectId}</if>" +
            "<if test=\"paperType != null \"> and paper_type= #{paperType}</if>" +
            "</script>")
    List<ExamPaper> page(ExamPaperPageRequestVM requestVM);

    @Select("select " + queryParam +
            "FROM t_exam_paper where deleted=0\n" +
            "      and task_exam_id is null\n" +
            "      and grade_level= #{level}\n" +
            "      and paper_type=#{paperType}")
    List<ExamPaper> taskExamPage(ExamPaperPageRequestVM requestVM);

    @Select("<script>" +
            " SELECT " + queryParam +
            "FROM t_exam_paper where deleted=0" +
            "<if test=\"subjectId != null \"> and subject_id=#{subjectId} </if>" +
            "<if test=\"levelId != null \"> and grade_level=#{levelId} </if>" +
            "and paper_type=#{paperType}" +
            "</script>")
    List<ExamPaper> studentPage(ExamPaperPageVM requestVM);

    @Select("<script>" +
            "SELECT id,name,limit_start_time limitStartTime,limit_end_time limitEndTime \n" +
            "FROM t_exam_paper " +
            "where deleted=0 \n" +
            "and paper_type= #{examPaperType} \n" +
            "and grade_level=#{gradeLevel} " +
            "<if test=\"examPaperType == 4 \"> and  #{dateTime} between limit_start_time  and limit_end_time </if>" +
            "ORDER BY id  desc limit 5 " +
            "</script>")
    List<PaperInfo> indexPaper(PaperFilter paperFilter);

    @Select("SELECT count(*) from t_exam_paper where deleted=0")
    Integer selectAllCount();

    @Select("SELECT create_time as name,COUNT(create_time) as value from\n" +
            "(\n" +
            "SELECT DATE_FORMAT(create_time,'%Y-%m-%d') as create_time from t_exam_paper\n" +
            "WHERE  deleted=0  and create_time  between  #{startTime}  and  #{endTime}\n" +
            ") a\n" +
            "GROUP BY create_time")
    List<KeyValue> selectCountByDate(@Param("startTime") Date startTime, @Param("endTime") Date endTime);

    @Update("<script>" +
            "update t_exam_paper set task_exam_id = #{taskId} where id in" +
            "<foreach item=\"id\" collection=\"paperIds\" open=\"(\" separator=\",\" close=\")\" >" +
            "      #{id}\n" +
            "</foreach>" +
            "</script>")
    int updateTaskPaper(@Param("taskId") Integer taskId, @Param("paperIds") List<Integer> paperIds);

    @Update("<script>" +
            "update t_exam_paper set task_exam_id = null where id in" +
            "<foreach item=\"id\" collection=\"paperIds\" open=\"(\" separator=\",\" close=\")\">" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int clearTaskPaper(@Param("paperIds") List<Integer> paperIds);
}
